package workflow.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.hibernate.Query;
import org.springside.core.dao.HibernateEntityDao;

import workflow.model.ProcessNode;

import common.PageInfo;

public class ProcessNodeDao extends HibernateEntityDao<ProcessNode>{
	public List<ProcessNode> findList(HashMap<String, String> hm, PageInfo pageInfo){
		try{
		
		String listHql = "FROM " + ProcessNode.class.getName() + " po WHERE (1=1) ";
		String countHql = null;
		/*------------查询条件-------------*/
		//注意以设值方式组装查询条�?,参�?�PageItemManager 方法:findList
		
		String column1 = (String)hm.get("id");
		String proInsId = (String)hm.get("proInsId");
		String name = (String)hm.get("name");
		String assignee = (String)hm.get("assignee");
		String formUrl = (String)hm.get("formUrl");
		String createDate = (String)hm.get("createDate");
		String dueDate = (String)hm.get("dueDate");
		String hbTask = (String)hm.get("hbTask");
		String nodeFlag = (String)hm.get("nodeFlag");
		String nodeType = (String)hm.get("nodeType");
		String expression = (String)hm.get("expression");
		String status = (String)hm.get("status");
		String visiable = (String)hm.get("visiable");


		int count[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

		if(column1 != null && !"".equals(column1.trim())){
			listHql += " AND id =:id";
			count[0] = 1;
		}

		if(proInsId != null && !"".equals(proInsId.trim())){
			listHql += " AND proInsId =:proInsId";
			count[1] = 1;
		}

		if(name != null && !"".equals(name.trim())){
			listHql += " AND name LIKE:name";
			count[2] = 1;
		}

		if(assignee != null && !"".equals(assignee.trim())){
			listHql += " AND assignee =:assignee";
			count[3] = 1;
		}

		if(formUrl != null && !"".equals(formUrl.trim())){
			listHql += " AND formUrl LIKE:formUrl";
			count[4] = 1;
		}

		if(createDate != null && !"".equals(createDate.trim())){
			listHql += " AND createDate =:createDate";
			count[5] = 1;
		}

		if(dueDate != null && !"".equals(dueDate.trim())){
			listHql += " AND dueDate =:dueDate";
			count[6] = 1;
		}

		if(hbTask != null && !"".equals(hbTask.trim())){
			listHql += " AND hbTask LIKE:hbTask";
			count[7] = 1;
		}

		if(nodeFlag != null && !"".equals(nodeFlag.trim())){
			listHql += " AND nodeFlag LIKE:nodeFlag";
			count[8] = 1;
		}

		if(nodeType != null && !"".equals(nodeType.trim())){
			listHql += " AND nodeType LIKE:nodeType";
			count[9] = 1;
		}

		if(expression != null && !"".equals(expression.trim())){
			listHql += " AND expression LIKE:expression";
			count[10] = 1;
		}

		if(status != null && !"".equals(status.trim())){
			listHql += " AND status LIKE:status";
			count[11] = 1;
		}

		if(visiable != null && !"".equals(visiable.trim())){
			listHql += " AND visiable LIKE:visiable";
			count[12] = 1;
		}

		listHql += " ORDER BY po.id DESC";
		countHql = listHql;
		countHql = countHql.replace("FROM " + ProcessNode.class.getName() + "", "SELECT COUNT(*) FROM " + ProcessNode.class.getName() + "");
		Query queryCount = this.createQuery(countHql);
		Query queryList = this.createQuery(listHql);

		if(count[0] == 1){
			queryList.setLong("column1", Long.valueOf(column1));
			queryCount.setLong("column1", Long.valueOf(column1));
		}

		if(count[1] == 1){
			queryList.setLong("proInsId", Long.valueOf(proInsId));
			queryCount.setLong("proInsId", Long.valueOf(proInsId));
		}

		if(count[2] == 1){
			queryList.setString("name", "%" + name + "%");
			queryCount.setString("name", "%" + name + "%");
		}

		if(count[3] == 1){
			queryList.setLong("assignee", Long.valueOf(assignee));
			queryCount.setLong("assignee", Long.valueOf(assignee));
		}

		if(count[4] == 1){
			queryList.setString("formUrl", "%" + formUrl + "%");
			queryCount.setString("formUrl", "%" + formUrl + "%");
		}

		if(count[5] == 1){
			queryList.setDate("createDate", (new java.text.SimpleDateFormat("yyyy-MM-dd")).parse(createDate));
			queryCount.setDate("createDate", (new java.text.SimpleDateFormat("yyyy-MM-dd")).parse(createDate));
		}

		if(count[6] == 1){
			queryList.setDate("dueDate", (new java.text.SimpleDateFormat("yyyy-MM-dd")).parse(dueDate));
			queryCount.setDate("dueDate", (new java.text.SimpleDateFormat("yyyy-MM-dd")).parse(dueDate));
		}

		if(count[7] == 1){
			queryList.setString("hbTask", "%" + hbTask + "%");
			queryCount.setString("hbTask", "%" + hbTask + "%");
		}

		if(count[8] == 1){
			queryList.setString("nodeFlag", "%" + nodeFlag + "%");
			queryCount.setString("nodeFlag", "%" + nodeFlag + "%");
		}

		if(count[9] == 1){
			queryList.setString("nodeType", "%" + nodeType + "%");
			queryCount.setString("nodeType", "%" + nodeType + "%");
		}

		if(count[10] == 1){
			queryList.setString("expression", "%" + expression + "%");
			queryCount.setString("expression", "%" + expression + "%");
		}

		if(count[11] == 1){
			queryList.setString("status", "%" + status + "%");
			queryCount.setString("status", "%" + status + "%");
		}

		if(count[12] == 1){
			queryList.setString("visiable", "%" + visiable + "%");
			queryCount.setString("visiable", "%" + visiable + "%");
		}



		//分页查询-START
		List countList = queryCount.list();
		int totalSize = 0;//总数
		if(countList.size() > 0){
			totalSize = Integer.valueOf(countList.get(0).toString());
			pageInfo.setTotalSize(totalSize);
		} else {
			pageInfo.setTotalSize(0);
		}
		
		queryList.setMaxResults(pageInfo.getPageSize());//设置查询数量
		queryList.setFirstResult(pageInfo.getPageNO() * pageInfo.getPageSize());//设置查询分段
		//分页查询-END
		
		return queryList.list();
		
		} catch (Exception e) {
			e.printStackTrace();
			return new ArrayList<ProcessNode>();
		}
	}
	
	public boolean deleteByIds(String ids){
		boolean isSuccessful = false;
		try{			
			String hql = "DELETE FROM " + ProcessNode.class.getName() + " po WHERE po.id IN (" + ids + ")";
			this.getHibernateTemplate().bulkUpdate(hql);
			isSuccessful = true;
		} catch (Exception e){
			e.printStackTrace();
		}
		
		return isSuccessful;
	}
}
